﻿using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDto.ConsumptionHistoryDtos;
using CozeDto.RechargeHistoryDtos;
using D.UtilFreesqlOrm;


namespace CozeDomain.Repositories
{
    public class RechargeHistoryEntityRepository : BaseRepository<RechargeHistoryEntity>, IRechargeHistoryEntityRepository
    {
        public RechargeHistoryEntityRepository(IFreeSql db) : base(db)
        {
        }

        public async Task<Tuple<long, List<RepRechargeHistoryInfo>>> QueryRechargeHistoryPagesAsync(ReqQueryRechargeHistoryPage reqQueryRechargeHistoryPage)
        {

            var where = _db.Select<RechargeHistoryEntity, UserEntity>()
                                                               .LeftJoin((a, b) => a.UserId == b.Id)
                                                               .Where((a, b) => a.TenantId == reqQueryRechargeHistoryPage.TenantId)
                                                               .WhereIf(!string.IsNullOrEmpty(reqQueryRechargeHistoryPage.Account), (a, b) => b.Account.Contains(reqQueryRechargeHistoryPage.Account));

            var dbres = await where.Count(out var totalCount)
                                                         .Page(reqQueryRechargeHistoryPage.PageNo, reqQueryRechargeHistoryPage.PageSize)
                                                         .ToListAsync((a, b) => new RepRechargeHistoryInfo()
                                                         {
                                                             Account = b.Account,
                                                             Amount = a.Amount, 
                                                             CreateTime = a.CreateTime,
                                                             CreateUser = a.CreateUser,
                                                             Description = a.Description,
                                                             Id = a.Id,
                                                             UpdateTime = a.UpdateTime,
                                                             UpdateUser = a.UpdateUser

                                                         });

            return new Tuple<long, List<RepRechargeHistoryInfo>>(totalCount, dbres);
        }
    }
}
